package day9;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TimerTask;

public class Main extends JPanel {

    static BufferedImage bg;
    static BufferedImage smallPlane;
    static BufferedImage bigPlane;
    static BufferedImage bee;
    static BufferedImage selfPlane;
    static BufferedImage selfPlane2;
    static BufferedImage bullet;
    static BufferedImage start;
    static BufferedImage pause;
    static BufferedImage gameOver;
    static {
        try {
           bg=ImageIO.read(Main.class.getResourceAsStream("pic/background.png"));
           smallPlane=ImageIO.read(Main.class.getResourceAsStream("pic/airplane.png"));
            bigPlane=ImageIO.read(Main.class.getResourceAsStream("pic/bigplane.png"));
           bee=ImageIO.read(Main.class.getResourceAsStream("pic/bee.png"));
            selfPlane=ImageIO.read(Main.class.getResourceAsStream("pic/hero0.png"));
            bullet=ImageIO.read(Main.class.getResourceAsStream("pic/bullet.png"));
            selfPlane2=ImageIO.read(Main.class.getResourceAsStream("pic/hero1.png"));
            start=ImageIO.read(Main.class.getResourceAsStream("pic/start.png"));
            pause=ImageIO.read(Main.class.getResourceAsStream("pic/pause.png"));
            gameOver=ImageIO.read(Main.class.getResourceAsStream("pic/gameover.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    ArrayList<BaseClass> base=new ArrayList<>();
    ArrayList<Bullet> bullets=new ArrayList<>();
    SelfPlane self=new SelfPlane();
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg,0,0,this);
        Font f = new Font("微软雅黑",Font.BOLD,15);
        g.setFont(f);
        g.drawString("分数:"+self.score,0,50);
        g.drawString("生命:"+self.blood,0,70);
        drawSelfPlane(g);
       if(currentState==RUNNING){
           paintEnemy(g);
           paintBullet(g);
       }
       if(currentState==START){
           g.drawImage(start,0,0,this);
       }
       if(currentState==PAUSE){
           paintEnemy(g);
           paintBullet(g);
           g.drawImage(pause,0,0,this);
       }
       if(currentState==GAME_OVER){
           paintEnemy(g);
           paintBullet(g);
          g.drawImage(gameOver,0,0,this);
       }
    }
    public void drawSelfPlane(Graphics g){
        g.drawImage(self.getBf(), self.getX(), self.getY(), this);
    }
    public void paintEnemy(Graphics g){
        for (int i = 0; i < base.size(); i++) {
            BaseClass n = base.get(i);
            g.drawImage(n.getBf(),n.getX(),n.getY(),this);
        }
    }
    public void paintBullet(Graphics g){
        for (int i = 0; i < bullets.size(); i++) {
            Bullet b = bullets.get(i);
            g.drawImage(b.getBf(),b.getX(),b.getY(),this);
        }
    }
    public void runBullet(){
        for (int i = 0; i < bullets.size(); i++) {
            Bullet b=bullets.get(i);
            b.move();
        }
    }
    public void runEnemy(){
        for (int i = 0; i < base.size(); i++) {
            BaseClass b=base.get(i);
            b.move();
        }
    }
    static final int START=0;
    static final int RUNNING=1;
    static final int PAUSE=2;
    static final int GAME_OVER=3;
    int currentState=START;
    public void action(){
        java.util.Timer timer=new java.util.Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(currentState==RUNNING){
                    shootBullet();
                    enemyRemove();
                    self.move();
                    runBullet();
                    bulletRemove();
                    runEnemy();
                    createEnemy();
                    collider();
                    selfPlaneCollder();
                    if(self.blood==0){
                        currentState=GAME_OVER;
                    }
                }
                repaint();
            }
        },20,40);
        MouseAdapter adapter = new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e) {
//                super.mouseClicked(e);
                currentState=RUNNING;
                if(currentState==GAME_OVER){
                    currentState=START;
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
//                super.mouseEntered(e);
                if(currentState==PAUSE){
                    currentState=RUNNING;
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
//                super.mouseExited(e);
                if(currentState==RUNNING){
                    currentState=PAUSE;
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
//                super.mouseMoved(e);
                if(currentState==RUNNING){
                    self.setX(e.getX()-self.getWidth()/2);
                    self.setY(e.getY()-self.getHeight()/2);
                }
            }
        };
        this.addMouseListener(adapter);
        this.addMouseMotionListener(adapter);
    }
    int selfcount=0;
    public void selfPlaneCollder() {
        for (int i = 0; i < base.size(); i++) {
            selfcount++;
            if(selfcount%20==0){
                BaseClass b=base.get(i);
                if(self.x<b.x+b.width&&self.x+self.width>b.x&&self.y<b.y+b.height&&self.y+self.height>b.y){
                    self.blood-=1;
                }
            }
        }
    }

    public void collider() {
        for (int i = 0; i < base.size(); i++) {
            for (int j = 0; j < bullets.size(); j++) {
                Bullet b=bullets.get(j);
                BaseClass ba=base.get(i);
                if(ba.getY()<b.getY()&&ba.getY()+ba.getHeight()>b.getY()&&b.getX()>ba.getX()&&b.getX()<ba.getX()+ba.getWidth()){
                    ba.blood=ba.blood-1;
                    if(ba.blood==0){
                        base.remove(i);
                        if(ba instanceof ScoreState){
                            ScoreState ba1 = (ScoreState) ba;
                            self.score+=ba1.addScore();
                        }
                        if(ba instanceof Award){
                            Award aw=(Award) ba;
                            if(aw.AddAward()==0){
                                self.doubleBulletShoot();
                            }
                            else if(aw.AddAward()==1){
                                self.blood+=1;
                            }
                        }
//                        if(ba instanceof SmallPlane){
//                            self.score+=10;
//                        }
//                        else if(ba instanceof BigPlane){
//                            self.score+=50;
//                        }
//                        else {
//                            self.score+=5;
//                        }
                    }
                    bullets.remove(j);
                    j--;
                    break;
                }
            }
        }
    }

    private void bulletRemove() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet b=bullets.get(i);
            if(b.getY()<=0){
                bullets.remove(b);
                i--;
            }
        }
    }

    public void enemyRemove() {
        for (int i = 0; i < base.size(); i++) {
            BaseClass b=base.get(i);
            if(b.getY()>=HEIGHT){
                base.remove(b);
                i--;
            }
        }
    }

    int count=0;
    public void createEnemy() {
        count++;
        if (count%20==0){
            int ran= (int) (Math.random()*20);
            BaseClass b;
            if(ran==0){
                b=new Bee();
            }
            else if(ran>0&&ran<=5){
                b=new BigPlane();
            }
            else {
                b=new SmallPlane();
            }
            base.add(b);
        }
    }
    int shootcount=0;
    public void shootBullet(){
        shootcount++;
        if(shootcount%10==0){
            Bullet[] b=self.bulletshoot();
            for (int i = 0; i < b.length; i++) {
                bullets.add(b[i]);
            }
        }
    }

    static final int WIDTH=400;
    static final int HEIGHT=700;
    public static void main(String[] args) {
        JFrame jFrame=new JFrame();
        Main main=new Main();
        jFrame.add(main);
        main.action();
        jFrame.setSize(WIDTH,HEIGHT);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
